package com.lc.recovery.ui.activity.warehousehome;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.SstorageDoApi;
import com.lc.recovery.http.request.StorageAddApi;
import com.lc.recovery.http.response.PublicMsg;
import com.lc.recovery.http.response.StorageAddBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class StorageAddListActivity extends AppActivity {

    @BindView(R.id.rv_recoverymessage)
    RecyclerView rvRecoverymessage;
    @BindView(R.id.layout_refresh)
    TwinklingRefreshLayout layoutRefresh;

    private List<StorageAddBean.DataBean> dataBeanList = new ArrayList<>();
    private int page = 1;
    private int total;
    private BaseQuickAdapter<StorageAddBean.DataBean, BaseViewHolder> messageWarehousedapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        storage_totals(page);
        messageWarehousedapter = new BaseQuickAdapter<StorageAddBean.DataBean, BaseViewHolder>(R.layout.item_storageworker, dataBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, StorageAddBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_type, bean.getTitle());
                baseViewHolder.setText(R.id.tv_weight,bean.getWeights());
                baseViewHolder.setText(R.id.tv_chuku,"核验");

            }
        };

        View emptyview1 = View.inflate(getContext(), R.layout.empty_emptylayout, null);
        messageWarehousedapter.setEmptyView(emptyview1);
        rvRecoverymessage.setAdapter(messageWarehousedapter);
        messageWarehousedapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                storage_add_do(dataBeanList.get(position).getId()+"");

            }
        });

        layoutRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                storage_totals(page);


                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {

                page++;
                if (page < total) {
                    orderShowMore(page);
                } else {
                    refreshLayout.setEnableLoadmore(false);

                }





//                }

//         }
            }
        });

    }

    private void storage_add_do(String id) {
        EasyHttp.post(this)
                .api(new SstorageDoApi().setWorker_id(SPUtil.get(this,IntentKey.BUSINESSUSERID,"")).setId(id).setStorager_id(SPUtil.get(this,IntentKey.USERID,""))).request(new HttpCallback<PublicMsg>(this) {

            @Override
            public void onSucceed(PublicMsg data) {
                if (data.getCode() == 200) {

                  toast(data.getMsg());
                    storage_totals(page);
                }else {
                    toast(data.getMsg());
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_storageaddlist;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
    private void storage_totals(int page) {
        EasyHttp.post(this)
                .api(new StorageAddApi().setWorker_id(SPUtil.get(this,IntentKey.BUSINESSUSERID,""))).request(new HttpCallback<StorageAddBean>(this) {

            @Override
            public void onSucceed(StorageAddBean data) {
                if (data.getCode() == 200) {
                    total = data.getCounts();
                    dataBeanList.clear();
                    dataBeanList.addAll(data.getData());
                    messageWarehousedapter.setNewData(dataBeanList);
                    messageWarehousedapter.notifyDataSetChanged();

                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
    private void orderShowMore(int page) {
        EasyHttp.post(this)
                .api(new StorageAddApi().setWorker_id(SPUtil.get(this,IntentKey.BUSINESSUSERID,""))).request(new HttpCallback<StorageAddBean>(this) {

            @Override
            public void onSucceed(StorageAddBean data) {
                if (data.getCode() == 200) {
                    total = data.getCounts();
                    dataBeanList.addAll(data.getData());
                    messageWarehousedapter.setNewData(dataBeanList);
                    messageWarehousedapter.notifyDataSetChanged();

                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
}
